package com.class5.springboot.frame.repo.dto;

import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class ParamMap extends HashMap<String, Object> {

    private boolean isBlank(String name){
        if(this.get(name) == null){
            return true;
        }
        String a = this.get(name)+"";
        if(a.length() == 0){
            return true;
        }
        return false;
    }

    public int getInt(String name) {
        return isBlank(name) ? 0 : Integer.parseInt(this.get(name) + "");
    }

    public int getInt(String name, int def) {
        return isBlank(name) ? def : Integer.parseInt(this.get(name) + "");
    }

    public Integer getInteger(String name) {
        return isBlank(name) ? null : Integer.parseInt(this.get(name) + "");
    }

    public Long getLong(String name) {
        return isBlank(name) ? null : Long.parseLong(this.get(name) + "");
    }

    public String getString(String name) {
        return this.get(name) == null ? null : this.get(name) + "";
    }

    public Double getDouble(String name) {
        return isBlank(name) ? null : Double.parseDouble(this.get(name) + "");
    }

    public Boolean getBoolean(String name) {
        return isBlank(name) ? null : Boolean.parseBoolean(this.get(name) + "");
    }

    public int getPage() {
        return this.getInt("_page");
    }

    public void setPage(int page) {
        this.put("_page", page);
    }

    public int getPageSize() {
        return this.getInt("_pageSize");
    }

    public void setPageSize(int pageSize) {
        this.put("_pageSize", pageSize);
    }

    public static ParamMap get() {
        return new ParamMap();
    }
    public ParamMap put2(String key, Object obj) {
        super.put(key, obj);
        return this;
    }

    public ParamMap putAll2(Map map) {
        super.putAll(map);
        return this;
    }
    public static ParamMap get(JSONObject obj) {
        ParamMap p = new ParamMap();
        p.putAll(obj);
        return p;
    }
}